package com.apes.fico.finance.model.costcollect;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.*;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = OperatingCosts.CONST_TABLE_NAME)
public class OperatingCosts extends Model {
    public final static String CONST_TABLE_NAME = "gl_operating_costs";

    @Id
    @Column(name = "id", nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "门店经营成本", padding = 8 ,prefix = "JY")
    @Comment("编号")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
    @Comment("部门")
    private Dept dept;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_company_id", referencedColumnName = "id")
    @Comment("所属公司")
    private PartyRole deptCompany;

    @NotNull
    @Column(name = "year_month")
    @Comment("年月")
    private Date yearMonth;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cost_type", referencedColumnName = "id")
    @Comment("成本类型")
    private Enumeration costType;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "type_cost", referencedColumnName = "id")
    @Comment("成本类别")
    private TypeCost typeCost;

    @Column(name = "accrual_date")
    @Comment("计提时间")
    private Date accrualDate;

    @Column(name = "accrual_amt", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("计提金额")
    private double accrualAmt = 0;

    @Column(name = "real_date")
    @Comment("实际发生时间")
    private Date realDate;

    @Column(name = "real_amt", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("实际发生金额")
    private double realAmt = 0;

    @Column(name = "blunt_provision_date")
    @Comment("冲计提时间")
    private Date bluntProvisionDate;

    @Column(name = "blunt_provision_amt", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("冲计提金额")
    private double bluntProvisionAmt = 0;

    @Column(name = "blunt_actually_date")
    @Comment("冲实际发生时间")
    private Date bluntActuallyDate;

    @Column(name = "blunt_actually_amt", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("冲实际发生金额")
    private double bluntActuallyAmt = 0;

    @Column(name = "accrual_state")
    @Comment("计提状态") //done(完成)
    private String accrualState;

    @Column(name = "real_state")
    @Comment("实际发生状态")//done(完成)
    private String realState;

    @OneToMany(mappedBy = "operatingCosts", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<OperatingCostsItem> operatingCostsItemSet = new HashSet<>();

    public Set<OperatingCostsItem> getOperatingCostsItemSet() {
        if (operatingCostsItemSet == null) operatingCostsItemSet = new HashSet<>();
        for (OperatingCostsItem item : operatingCostsItemSet) {
            item.setOperatingCosts(this);
        }
        return operatingCostsItemSet;
    }

    @Column(name = "proof_digest")
    @Comment("凭证摘要")
    private String proofDigest;
}
